// pages/address/address.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: [],
    type:2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var type=options.type;
    that.setData({
      type:type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  chooseAddress:function(e){
    var that=this;
    var aid=e.currentTarget.dataset.id;
    var type =that.data.type;
    wx.setStorageSync('aid', aid);
    if(type == 1){
      setTimeout(function(){
        wx.navigateBack({
          
        })
      },500)
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var uid = wx.getStorageSync('uid')
    var param = {
      uid: uid
    }
    app.wxRequest('POST', "Index/addressList", param, (res) => {
      var data = res.data;
      that.setData({
        addressList: data
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },
  addAddressInfo: function () {
    wx.navigateTo({
      url: '../addAddress/addAddress',
    })
  },
  bindMorenAddress: function (e) {
    var that = this;
    var aid = e.currentTarget.dataset.id;
    var param = {
      aid: aid
    }
    app.wxRequest('POST', "Index/setUpMoren", param, (res) => {
      var code = res.code;
      if (code == 1) {
        wx.showToast({
          title: '操作成功',
          icon: "none",
          duration: 1500
        })
        that.onShow();
      } else {
        wx.showToast({
          title: '操作失败,请重试',
          icon: "none",
          duration: 1500
        })
      }
    }, (err) => {
      console.log(err.errMsg)
    })
    console.log(e)
  },
  editAddressInfo: function (e) {
    var that = this;
    var aid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../editAddress/editAddress?aid=' + aid,
    })
  },
  delAddressInfo: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '删除收货地址',
      content: '确定要删除该收货地址吗？',
      showCancel: true, //是否显示取消按钮
      cancelText: "取消", //默认是“取消”
      cancelColor: '#9C9C9C', //取消文字的颜色
      confirmText: "确定", //默认是“确定”
      confirmColor: '#1AA034', //确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          var param = {
            aid: id
          };
          app.wxRequest('POST', "Index/delAddressInfo", param, (res) => {
            var code = res.code;
            if (code == 1) {
              wx.showToast({
                title: '操作成功',
                icon: "none",
                duration: 1500
              })
              that.onShow();
            } else {
              wx.showToast({
                title: '操作失败,请重试',
                icon: "none",
                duration: 1500
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
        }
      },
      fail: function (res) {}, //接口调用失败的回调函数
      complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})